package com.ews.onlineexchange.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class TimeCloseBulletinList extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Date chooseTime;
	private String bulletinId;//用于排除当前公告
	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<Bulletin> entity ) {
		
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		restrictions.add( cb.notEqual(entity.get("id"),  bulletinId == null ? "null" : bulletinId));
		restrictions.add( cb.greaterThanOrEqualTo( entity.get( "chooseTime" ), getStartingChooseTime() ) );
		restrictions.add( cb.lessThanOrEqualTo( entity.get( "chooseTime" ), getEndingChooseTime() ) );
		
		return restrictions;
		
	}

	public TimeCloseBulletinList() {
		setMaxResults( null );
	}

	private Date getEndingChooseTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime( getChooseTime() );
		cal.add( Calendar.MINUTE, 10 );
		return cal.getTime();
	}

	private Date getStartingChooseTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime( getChooseTime() );
		cal.add( Calendar.MINUTE, -10 );
		return cal.getTime();
	}

	public Date getChooseTime() {
		return chooseTime;
	}

	public void setChooseTime( Date chooseTime ) {
		this.chooseTime = chooseTime;
	}

	public String getBulletinId() {
		return bulletinId;
	}

	public void setBulletinId(String bulletinId) {
		this.bulletinId = bulletinId;
	}
	
	
	
}
